from sqlalchemy import delete, select, update, func
from sqlalchemy.ext.asyncio import AsyncSession
from module_finance.entity.do.unitInfo_do import UnitInfo
from module_finance.entity.vo.unitInfo_vo import UnitinfoModel, UnitinfoPageQueryModel
from utils.page_util import PageUtil


class UnitinfoDao:
    """
    填报单位信息模块数据库操作层
    """

    @classmethod
    async def get_unitInfo_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据主键获取填报单位信息详细信息

        :param db: orm对象
        :param id: 主键
        :return: 填报单位信息信息对象
        """
        unitInfo_info = (
            (
                await db.execute(
                    select(UnitInfo)
                    .where(
                        UnitInfo.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return unitInfo_info

    @classmethod
    async def get_unitInfo_detail_by_info(cls, db: AsyncSession, unitInfo: UnitinfoModel):
        """
        根据填报单位信息参数获取填报单位信息信息

        :param db: orm对象
        :param unitInfo: 填报单位信息参数对象
        :return: 填报单位信息信息对象
        """
        unitInfo_info = (
            (
                await db.execute(
                    select(UnitInfo).where(
                        UnitInfo.id == unitInfo.id if unitInfo.id else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return unitInfo_info

    @classmethod
    async def get_unitInfo_list(cls, db: AsyncSession, query_object: UnitinfoPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取填报单位信息列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 填报单位信息列表信息对象
        """
        query = (
            select(UnitInfo)
            .where(
                UnitInfo.compname.like(f'%{query_object.compname}%') if query_object.compname else True,
                UnitInfo.year == query_object.year if query_object.year else True,
                UnitInfo.leader == query_object.leader if query_object.leader else True,
                UnitInfo.status == query_object.status if query_object.status else True,
                UnitInfo.department.like(f'%{query_object.department}%') if query_object.department else True,
                UnitInfo.main_type == query_object.main_type if query_object.main_type else True,
                UnitInfo.nine_table == query_object.nine_table if query_object.nine_table else True,
                UnitInfo.zpbg == query_object.zpbg if query_object.zpbg else True,
            )
            .order_by(UnitInfo.id)
            .distinct()
        )
        unitInfo_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return unitInfo_list

    @classmethod
    async def add_unitInfo_dao(cls, db: AsyncSession, unitInfo: UnitinfoModel):
        """
        新增填报单位信息数据库操作

        :param db: orm对象
        :param unitInfo: 填报单位信息对象
        :return:
        """
        db_unitInfo = UnitInfo(**unitInfo.model_dump(exclude={'guid', }))
        db.add(db_unitInfo)
        await db.flush()

        return db_unitInfo

    @classmethod
    async def edit_unitInfo_dao(cls, db: AsyncSession, unitInfo: dict):
        """
        编辑填报单位信息数据库操作

        :param db: orm对象
        :param unitInfo: 需要更新的填报单位信息字典
        :return:
        """
        await db.execute(update(UnitInfo), [unitInfo])

    @classmethod
    async def delete_unitInfo_dao(cls, db: AsyncSession, unitInfo: UnitinfoModel):
        """
        删除填报单位信息数据库操作

        :param db: orm对象
        :param unitInfo: 填报单位信息对象
        :return:
        """
        await db.execute(delete(UnitInfo).where(UnitInfo.id.in_([unitInfo.id])))

